package com.ruoyi.carrepair.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carrepair.domain.QxOperatelog;
import com.ruoyi.carrepair.mapper.QxOperatelogMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.zjc.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carrepair.mapper.QxCarMapper;
import com.ruoyi.carrepair.domain.QxCar;
import com.ruoyi.carrepair.service.IQxCarService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class QxCarServiceImpl implements IQxCarService 
{
    @Autowired
    private QxCarMapper qxCarMapper;
    @Autowired
    private QxOperatelogMapper qxOperatelogMapper;

    /**
     * 查询车辆管理
     * 
     * @param id 车辆管理主键
     * @return 车辆管理
     */
    @Override
    public QxCar selectQxCarById(String id)
    {
        return qxCarMapper.selectQxCarById(id);
    }

    /**
     * 查询车辆管理列表
     * 
     * @param qxCar 车辆管理
     * @return 车辆管理
     */
    @Override
    public List<QxCar> selectQxCarList(QxCar qxCar)
    {
        qxCar.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        return qxCarMapper.selectQxCarList(qxCar);
    }

    /**
     * 新增车辆管理
     * 
     * @param qxCar 车辆管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQxCar(QxCar qxCar)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();

        qxCar.setRowVersion(0L);
        qxCar.setIsDeleted(0L);
        qxCar.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        qxCar.setCreateID(SecurityUtils.getLoginUser().getUserId());
        qxCar.setCreator(SecurityUtils.getLoginUser().getUsername());
        qxCar.setCreateDate(new Date());
        qxCar.setId(String.valueOf(new SnowflakeIdWorker(5,8).nextId()));
        int num = qxCarMapper.insertQxCar(qxCar);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "新增了一条车辆/用户信息：【" + qxCar.getCarno()+"、"+qxCar.getChejiahao()+"、"+qxCar.getKname()+ "】");
        qxOperatelog.setCategory("1");
        qxOperatelog.setMenuModel("车辆/用户信息");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 修改车辆管理
     * 
     * @param qxCar 车辆管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateQxCar(QxCar qxCar)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();

        int num = qxCarMapper.updateQxCar(qxCar);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "修改了一条车辆/用户信息：【" + qxCar.getCarno()+"、"+qxCar.getChejiahao()+"、"+qxCar.getKname()+ "】");
        qxOperatelog.setCategory("2");
        qxOperatelog.setMenuModel("车辆/用户信息");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 批量删除车辆管理
     * 
     * @param ids 需要删除的车辆管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteQxCarByIds(String[] ids)
    {
        String idsStr="";
        for(String id:ids){
            idsStr = idsStr + id + ",";
        }
        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();

        int num = qxCarMapper.deleteUpdQxCarByIds(ids);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "删除了车辆/用户信息。车辆/用户信息id：【" + idsStr+ "】");
        qxOperatelog.setCategory("3");
        qxOperatelog.setMenuModel("车辆/用户信息");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 删除车辆管理信息
     * 
     * @param id 车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteQxCarById(String id)
    {
        return qxCarMapper.deleteQxCarById(id);
    }
}
